package com.tpo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tpo.base.Menus;
import com.tpo.base.ToolBar;
import com.tpo.model.Tpo_module;
import com.tpo.model.Tpo_user;
import com.tpo.model.User;
import com.tpo.service.TpoModuleManagerService;
import com.tpo.service.UserManagerService;
import com.tpo.service.UserServiceI;
import com.tpo.util.CookieUtil;


@Controller
@RequestMapping("/userController")
public class UserController {
	@Autowired
	private UserServiceI userService;
	
	@RequestMapping("/showUser")
	public String showUser(HttpServletRequest request) {
		User u = new User();
		u.setName("zhangsan");
		request.setAttribute("user", u);
		return "showUser";
	}
	
	@Autowired
	private UserManagerService userManagerService;
	
	@Autowired
	private TpoModuleManagerService tpoModuleManagerService;
	
	private CookieUtil cookieUtil = new CookieUtil();
	
	
	public CookieUtil getCookieUtil() {
		return cookieUtil;
	}
	public void setCookieUtil(CookieUtil cookieUtil) {
		this.cookieUtil = cookieUtil;
	}
	
	@RequestMapping("/userfind")
	public String userfind(String username,String password,Model model,HttpSession session,HttpServletResponse response) throws Exception {
		String name = new String(username.getBytes("ISO-8859-1"),"UTF-8");
		Tpo_user user = userManagerService.FindUserByName(name);
		
		if(user != null&&user.getPassword().equals(password)){
			
			cookieUtil.addCookie(response, "loginname", user.getUsername(), -1);
			cookieUtil.addCookie(response, "password", user.getPassword(), -1);
			session.setAttribute(name, name);
			session.setAttribute("user", user);
			session.setAttribute("loginname", user.getUsername());
			session.setAttribute("password", user.getPassword());
			return "main";
		}else{
			model.addAttribute("message","用户名或密码错误!");
			return "index";
		}
	}
	
	@RequestMapping("/showmenus")
	public @ResponseBody List<Menus> showMenus(Model model,HttpSession session){
		Tpo_user user = (Tpo_user)session.getAttribute("user");
		
		List<Menus> menus = new ArrayList<Menus>();
		List<ToolBar> toolBars = new ArrayList<ToolBar>();
		
		List<Tpo_module> tpo_modules = tpoModuleManagerService.findMenuByUserUid(user.getUid());
		if(tpo_modules !=null &&tpo_modules.size()>0){
			for(Tpo_module tpo_module : tpo_modules){
				Menus moduleInfo = new Menus();
				moduleInfo.setId(tpo_module.getId());
				moduleInfo.setMenuid(tpo_module.getId().toString());
				moduleInfo.setMenuname(tpo_module.getModulename());
				moduleInfo.setMenuno(tpo_module.getId().toString());
				moduleInfo.setMenuurl(tpo_module.getModuleurl());
				moduleInfo.setMenuparentno(tpo_module.getPid().toString());
				moduleInfo.setText(tpo_module.getModulename());
				List<Menus> subMenus = new ArrayList<Menus>();
				List<Tpo_module> tpo_submodules = tpoModuleManagerService.findSubMenuById(tpo_module.getId());
				if(tpo_submodules !=null && tpo_submodules.size()>0){
					for(Tpo_module subTpo_module : tpo_submodules){
						Menus subModuleInfo = new Menus();
						subModuleInfo.setId(subTpo_module.getId());
						subModuleInfo.setMenuid(subTpo_module.getId().toString());
						subModuleInfo.setMenuname(subTpo_module.getModulename());
						subModuleInfo.setMenuno(subTpo_module.getId().toString());
						subModuleInfo.setMenuurl(subTpo_module.getModuleurl());
						subModuleInfo.setMenuparentno(subTpo_module.getPid().toString());
						subModuleInfo.setText(subTpo_module.getModulename());
						subMenus.add(subModuleInfo);
					}
				}
				moduleInfo.setChildren(subMenus);
				menus.add(moduleInfo);
			}
		}
		return menus;
	}
	
	@RequestMapping("/loginout")
	public String Loginout(HttpSession session) {
		session.removeAttribute("user");
		session.removeAttribute("username");
		return "index";
	}
	
	@RequestMapping("/ChangePasswordPage")
	public String ChangePasswordPage(HttpSession session,Model model,HttpServletRequest request) {
		cookieUtil.getCookie(request, "admin");
		session.getAttribute("loginname");
		return "PasswordDetail";
	}
	
	@RequestMapping("/changepassword")
	public  @ResponseBody Map<String, String> Changepassword(String newpassword,HttpSession session) {
		Tpo_user user = new Tpo_user();
		String username = (String)session.getAttribute("loginname");
		user.setUsername(username);
		user.setPassword(newpassword);
		userManagerService.updatePassword(user);
		Map<String, String> map = new HashMap<String, String>(1); 
		map.put("success", "true");  
		return map;
	}
	
}
